package demo.hdz.mediacodecdecode.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import demo.hdz.mediacodecdecode.R;

/**
 * @author hdz
 * @date 2022/06/23 14:56
 */
public class ListDialogAdapter extends BaseAdapter {
    private final Context context;
    private final List<String> data;
    private int checked;


    public ListDialogAdapter(Context context, List<String> data, int curIndex) {
        this.context = context;
        this.data = data;
        this.checked = curIndex;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.dialog_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tv = (TextView) convertView.findViewById(R.id.text);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv.setText(data.get(position));

        if (position == checked) {
            viewHolder.img.setVisibility(View.VISIBLE);
        } else {
            viewHolder.img.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    public void setChecked(int index) {
        this.checked = index;
    }

    private static class ViewHolder {
        TextView tv;
        ImageView img;
    }
}
